#include "Arduino.h"
#include "WiFi.h"

#include "drv_cfg_tool.h"
#include "dd.h"

bool drv_wifi_init(void) {
  if  (!drv_cfg_tool_init()) {
    return false;
  }

  switch (pWiFi_Config->WiFi_mode) {
    case 0:
      WiFi.mode(WIFI_MODE_STA);
      break;
      
    case 1:
      WiFi.mode(WIFI_MODE_AP);
      break;
      
    case 2:
      WiFi.mode(WIFI_MODE_APSTA);
      break;
    
    default:
      return false;
      break;
  }

  if ((pWiFi_Config->WiFi_mode == 0) || (pWiFi_Config->WiFi_mode == 2)) {
    WiFi.begin(pWiFi_Config->client_ssid, pWiFi_Config->client_password);
  }

  if ((pWiFi_Config->WiFi_mode == 1) || (pWiFi_Config->WiFi_mode == 2)) {
    WiFi.softAP(pWiFi_Config->ap_ssid, pWiFi_Config->ap_password);
    IPAddress ap_ip = IPAddress(192, 168, 114, 1);
    IPAddress ap_gateway = IPAddress(192, 168, 114, 0);
    IPAddress ap_mask = IPAddress(255, 255, 255, 0);
    WiFi.softAPConfig(ap_ip, ap_gateway, ap_mask);
  }

  return true;
}
